﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace SimpleTFTP
{
    internal class AckPacket : BasePacket
    {
        public short BlockNo { get; set; }

        public AckPacket(short blockNo)
        {
            this.OpCode = OpCode.ACK;
            this.BlockNo = blockNo;
        }

        public AckPacket(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                this.OpCode = (OpCode)Utilities.ReadInt16(ms);
                this.BlockNo = (short)Utilities.ReadInt16(ms);
            }
        }

        public override byte[] ToByteArray()
        {
            List<byte> result = new List<byte>();

            result.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)this.OpCode)));
            result.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.BlockNo)));

            return result.ToArray();
        }
    }
}